package other._1;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;

public class ShowBull extends JFrame implements Runnable {
    private List<Point> list;
    public ShowBull( List<Point> list) {
        this.list = list;
        setTitle("凸包问题");
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - 1000) / 2, (screenSize.height - 1000) / 2, 1000, 1000);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
        setVisible(true);
        new Thread(this).start();
    }

    @Override
    public void paint(Graphics gp) {
        BufferedImage img = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = (Graphics2D) img.getGraphics();
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        // 画背景
        g.setColor(new Color(243, 243, 243));
        g.fillRect(0, 0, 1000, 1000);

        // 画圆
        g.setStroke(new BasicStroke(5f));
        Ellipse2D circle = new Ellipse2D.Double();
        g.setColor(new Color(10, 57, 10));
        circle.setFrameFromCenter(500, 500, 1000, 1000);

        //随机点生成在半径0-500的圆上
        for (Point point : list) {
            int X = point.x;
            int Y = point.y;
            circle.setFrameFromCenter(X, Y, X + 5, Y + 5);
            g.fill(circle);//点生成在半径0-500的圆上
        }

        QuickBull bull = new QuickBull();
        List<Integer> xx = new ArrayList<>();
        List<Integer> yy = new ArrayList<>();
        //第三种 分治法
        List<Line> eval = bull.handle(list);
        System.out.println("集合points中满足凸包的点集为：");
        for (Line line : eval) {
            System.out.println("(" + line.p1.getX() + "," + line.p1.getY() + ")");
            xx.add((int) line.p1.getX());
            yy.add((int) line.p1.getY());
        }
        // 坐标相连
        for (int i = 0; i < xx.size(); i++) {
            if (i == xx.size() - 1) {
                g.drawLine(xx.get(i), yy.get(i), xx.get(0), yy.get(0));
            } else {
                g.drawLine(xx.get(i), yy.get(i), xx.get(i + 1), yy.get(i + 1));
            }
        }
        gp.drawImage(img, 0, 0, img.getWidth(), img.getHeight(), null);
    }

    @Override
    public void run() {
        repaint();
    }
}
